﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Runtime.InteropServices;

namespace LanguageStudy
{
    public partial class WSForm : Form
    {
        Font m_fntUsing = null;

        ArrayList m_alWords = null;
        int m_nCurShowID;
        int m_nShowCount;
        int m_nMinWidth;
        Size m_szShowArea;
        int m_nChooseID;

        Point m_atMouse;

        public WSForm(WordsHandle wh)
        {
            InitializeComponent();

            m_fntUsing = new Font("Arial", 20);

            m_alWords = wh.RandomWords(0);
            
            m_nCurShowID = 0;
            m_nShowCount = 5;
            m_nMinWidth = btnExit.Location.X + btnExit.Width + btnNext.Location.X;

            m_atMouse.X = -1;
        }

        ~WSForm()
        {
            m_fntUsing.Dispose();

            m_alWords.Clear();
        }

        private void WSForm_Load(object sender, EventArgs e)
        {
            m_szShowArea = new Size(m_nMinWidth, 100);
            this.Size = new Size(m_szShowArea.Width, m_szShowArea.Height);

            btnNext.Location = new Point(btnNext.Location.X, m_szShowArea.Height);
            btnReset.Location = new Point(btnReset.Location.X, m_szShowArea.Height);
            btnExit.Location = new Point(btnExit.Location.X, m_szShowArea.Height);
            nudTimer.Location = new Point(nudTimer.Location.X, m_szShowArea.Height);

            this.Opacity = 0.5;

            m_nChooseID = -1;
            this.btnExplanation.Visible = false;
        }

        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        private const int WM_SYSCOMMAND = 0x0112;   // 点击窗口左上角那个图标时的系统信息 
        private const int SC_MOVE = 0xF010;         // 移动信息 
        private const int HTCAPTION = 0x0002;       // 表示鼠标在窗口标题栏时的系统信息 
        private void WSForm_MouseDown(object sender, MouseEventArgs e)
        {
            // 首先释放鼠标焦点捕获，这样就不会再发出WM_NCHITTEST消息
            ReleaseCapture();
            // 然后向当前窗体发送消息，消息是移动+表示鼠标在标题栏上
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
        }

        private void WSForm_MouseEnter(object sender, EventArgs e)
        {
            this.Size = new Size(this.Width, m_szShowArea.Height + btnNext.Height + 10);
            this.Opacity = 1.0;
        }

        private void WSForm_MouseLeave(object sender, EventArgs e)
        {
            if (
                Control.MousePosition.X >= this.Location.X &&
                Control.MousePosition.X <= this.Location.X + this.Width &&
                Control.MousePosition.Y >= this.Location.Y &&
                Control.MousePosition.Y <= this.Location.Y + this.Height)
                return;
            else
            {
                this.Size = new Size(this.Width, m_szShowArea.Height);
                this.Opacity = 0.5;
            }

            m_atMouse.X = -1;
            this.btnExplanation.Enabled = false;
        }

        private void WSForm_MouseMove(object sender, MouseEventArgs e)
        {
            this.btnExplanation.Enabled = true;
            m_atMouse = e.Location;
            this.Refresh();
        }

        private void WSForm_Paint(object sender, PaintEventArgs e)
        {
            if (m_alWords.Count > m_nCurShowID)
            {
                string _strWord;
                SizeF _szfWord;
                int _nHeight = 0;
                int _nWidth = 0;
                for (int _i = 0; _i < m_nShowCount; _i++)
                {
                    _strWord = m_alWords[m_nCurShowID + _i].ToString();
                    e.Graphics.DrawString(_strWord, m_fntUsing, Brushes.Blue, 0, _nHeight);

                    _szfWord = e.Graphics.MeasureString(_strWord, m_fntUsing);
                    _nHeight += (int)_szfWord.Height + 10;
                    if (_szfWord.Width > _nWidth)
                        _nWidth = (int)_szfWord.Width;
                }

                // 计算文字高度
                _szfWord = e.Graphics.MeasureString("AAAA", m_fntUsing);

                // 设置窗口尺寸
                Size _szOld = new Size(m_szShowArea.Width, m_szShowArea.Height);

                m_szShowArea = new Size(Math.Max(_nWidth, m_nMinWidth), _nHeight);
                this.Size = new Size(this.Width + (m_szShowArea.Width - _szOld.Width),
                                     this.Height + (m_szShowArea.Height - _szOld.Height));

                // 计算词意按钮位置
                if (this.btnExplanation.Enabled && m_atMouse.Y >= 0)
                {
                    m_nChooseID = -1;
                    for (int _i = 0; _i < m_nShowCount; _i++)
                    {
                        int _nTop = _i * ((int)_szfWord.Height + 10);

                        if (m_atMouse.Y >= _nTop &&
                            m_atMouse.Y <= _nTop + (int)_szfWord.Height)
                        {
                            this.btnExplanation.Visible = true;
                            m_nChooseID = _i;
                            break;
                        }
                    }

                    if (m_nChooseID >= 0)
                    {
                        this.btnExplanation.Visible = true;
                        this.btnExplanation.Top = m_nChooseID * ((int)_szfWord.Height + 10) +
                            ((int)_szfWord.Height - this.btnExplanation.Height) / 2;
                        this.btnExplanation.Left = this.Size.Width - this.btnExplanation.Size.Width - 10;
                    }
                    else
                    {
                        this.btnExplanation.Visible = false;
                    }
                }

                // 设置按钮位置
                btnNext.Location = new Point(btnNext.Location.X, m_szShowArea.Height);
                btnReset.Location = new Point(btnReset.Location.X, m_szShowArea.Height);
                btnExit.Location = new Point(btnExit.Location.X, m_szShowArea.Height);
                nudTimer.Location = new Point(nudTimer.Location.X, m_szShowArea.Height);

                // 设置按钮状态
                btnNext.Enabled = true;
                btnReset.Enabled = true;
            }
            else
            {
                btnNext.Enabled = false;
                if (m_alWords.Count == 0)
                    btnReset.Enabled = false;
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            m_nCurShowID += m_nShowCount;
            if (this.timer.Enabled)
            {
                this.timer.Stop();
                this.timer.Start();
            }
            this.Refresh();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            m_nCurShowID = 0;
            if (this.timer.Enabled)
            {
                this.timer.Stop();
                this.timer.Start();
            }
            this.Refresh();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void nudTimer_ValueChanged(object sender, EventArgs e)
        {
            int _nValue = Decimal.ToInt32(this.nudTimer.Value);

            if (_nValue == 0)
            {
                this.timer.Enabled = false;
                this.timer.Stop();
            }
            else
            {
                this.timer.Enabled = true;
                this.timer.Interval = _nValue * 60 * 1000;
                this.timer.Start();
            }
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            m_nCurShowID += m_nShowCount;
            this.Refresh();
        }

        private void btnExplanation_Click(object sender, EventArgs e)
        {
            WordForm wf = new WordForm((Word)m_alWords[m_nCurShowID + m_nChooseID]);
            wf.ShowDialog();
        }
    }
}
